<?php
/**
 * Display Partners List
 */
use Fox\SalesDistribution\Sales\Partner\Role;
use Doctrine\DBAL\Connection;
use Fox\Common\Customer;
$ajax = Xview::instance()->val("xvtrs");
$mode = Xview::instance()->val("mode");

$grid = Xview::instance()->component("customer", "partners",array('mode' => $mode))->item("gPartners"); 
if(!empty($ajax))
{
	$uniqueData = null;
	$data = $grid->get();
	if(is_array($data))
	{
		foreach($data as $key => $line)
		{
			if(empty($line['partnerid']))
				continue;
			$uniqueData[$line['partnerid']] = $line;
		}
	}
	$codes = Xview::instance()->val("codes");
	if(is_array($codes))
	{
		$finder = new Customer();
		$customers = $finder->find(new Fox\Dao\SqlFragment("customerid IN (?)",array($codes),array(Connection::PARAM_INT_ARRAY)));
		foreach($customers as $customer)
		{
			$data = $customer->getData();
			$data['role'] = Role::CONTACT_PERSON;
			$data['partnerid'] = $customer->getId();
			$uniqueData[$customer->getId()] = $data;
		}
	}
	$grid->setDatas($uniqueData);
	echo $grid->Display();
}